home *** CD-ROM | disk | FTP | other *** search
- #ifndef __EXCEPTIONS__
- #define __EXCEPTIONS__
- #pragma once
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
- #ifndef __APPLEEVENTS__
- #include <AppleEvents.h>
- #endif
-
- #include <stddef.h>
- #include <stdarg.h>
- #include <limits.h>
- #include <string.h>
-
- //
- // Our error codes
- //
- const OSErr ePointerNil = -110;
- const OSErr eGeneralErr = -1;
- const OSErr eAssertionFailure = -30000;
- const OSErr eRequirementNotMet = -30001;
- const OSErr eValueOutOfRange = -30002;
- const OSErr eIndexOutOfRange = -30003;
- const OSErr eLinksCorrupt = -30004;
- const OSErr eDataCorrupt = -30005;
- const OSErr eWrongDataType = -30006;
- const OSErr eMenuIDNotMenuRID = -30007;
-
-
- #include "LocationInCode.h"
-
- // Note that this MUST be a macro to be useful. We may be able to get the
- // macsbug name later
-
- #define GetLocationInCode() LocationInCode(__LINE__, __FILE__)
-
- extern LocationInCode gLocationInCode;
-
- #define SETUP_LOCATION (gLocationInCode.fLineNum = __LINE__, gLocationInCode.fFileName = __FILE__, gLocationInCode)
- #define LOCATION_PARAM const LocationInCode& where = LOCATION
- #define LOCATION gLocationInCode
-
- //#define LOCATION_PARAM const LocationInCode& where = GetLocationInCode()
-
-
- //
- // Useful va_arg utilitiies
- //
- typedef OSStatus (*VAProc)(va_list);
-
- #define VADynamicCast(ap, T) (dynamic_cast<T>(va_arg(args, T)))
- #define VAStaticCast(ap, T) (static_cast<T> (va_arg(args, T)))
- #define VA_ARG(T, name, ap) T name = va_arg(ap, T)
- #define VA_ARG_DEFAULT(T, name, ap, val) T name = ap ? va_arg(ap, T) : val
-
-
- //------------------------------------------------------------------------------
-
- class Exception
- {
- protected:
- LocationInCode fThrowPoint;// Where was this thrown from
- OSStatus fStatus; // We'll use a long to handle QDErr, OSErr, etc.
-
- friend void ThrowErrNum(const LocationInCode& where, long err);
-
- public:
- Exception(
- const Exception& exc)
- :
- fThrowPoint(exc.fThrowPoint),
- fStatus(exc.fStatus)
- {
- }
-
- Exception(
- const LocationInCode& where,
- OSStatus status = eGeneralErr)
- :
- fThrowPoint(where),
- fStatus(status)
- {
- }
-
- virtual void AboutToThrow(void) const;
-
- virtual bool IsFatal(void) const; // should app quit?
- virtual bool IsSilent(void) const; // should we normally report it?
-
- virtual OSStatus GetStatus(void) const;
- virtual OSStatus SetStatus(OSStatus status);
- // virtual long GetMessage(void) const;
- // virtual long SetMessage(long message);
-
- virtual OSErr GetOSErr(void) const; // This is always a valid OSErr
- virtual bool GetErrorMessage(AEDesc& msg) const;
- virtual bool GetErrorString(Str255& msg) const;
-
- virtual void Log() const;
- virtual void Report() const;
- virtual OSErr GetAEParams( AERecord& evt, bool create) const;
- virtual bool PutErrorNumber(AERecord& evt) const;
- virtual bool PutErrorString(AERecord& evt) const;
-
- //
- // call these to provide better errors to AppleScript
- //
-
- virtual bool HasExpectedType() const;
- virtual void PutExpectedType(
- DescType expectedType,
- DescType actualType = 0,
- bool overwrite = false);
-
- virtual bool HasOffendingObject() const;
- virtual void PutOffendingObject(
- const AEDesc& objectSpec,
- bool overwrite = false);
-
- virtual bool HasOffendingParameter() const;
- virtual void PutOffendingParameter(
- AEKeyword param,
- bool overwrite = false);
-
- virtual bool HasErrorParam(AEKeyword param) const;
- virtual void PutErrorParamPtr(
- AEKeyword param,
- DescType type,
- const void* data,
- Size size,
- bool overwrite = false);
- virtual void PutErrorParamDesc(
- AEKeyword param,
- const AEDesc& desc,
- bool overwrite = false);
- //
- // for compatibility with integer-based systems
- //
- inline operator long() { return this->GetStatus(); }
-
- //
- // these routines pare the possible number of exceptions thrown down
- // to something reasonable.
- //
- static OSStatus vStandardizeExceptions(VAProc proc, va_list arg) throw(Exception&);
- static OSStatus StandardizeExceptions(VAProc proc, ...) throw(Exception&);
- static void ReportExceptions(VAProc proc, ...) throw(Exception&);
- static OSErr CatchAEErrors(AppleEvent* evt, VAProc proc, ...) throw();
- static OSErr CatchOSErrors(VAProc proc, ...) throw();
- static OSStatus CatchOSStatus(VAProc proc, ...) throw();
- static void MakeTextDesc(AEDesc& desc, const char *cstr);
- static void MakeTextDesc(AEDesc& desc, ConstStr255Param pstr);
-
- typedef void (*ReportExceptionProcPtr)(const Exception& exc);
-
- public:
- static ReportExceptionProcPtr GetReportProc() { return gReportProc; }
- static ReportExceptionProcPtr SetReportProc(ReportExceptionProcPtr newProc);
-
- #if qDebug
- static inline bool LogExceptions() { return gLogExceptions; }
- static inline bool BreakOnThrow() { return gBreakOnThrow; }
- static bool SetLogExceptions(bool newState);
- static bool SetBreakOnThrow(bool newState);
- #else
- static inline bool LogExceptions() { return false; }
- static inline bool BreakOnThrow() { return false; }
- static inline bool SetLogExceptions(bool /*newState*/) { return false; }
- static inline bool SetBreakOnThrow(bool /*newState*/) { return false; }
- #endif
-
- private:
- static ReportExceptionProcPtr gReportProc;
-
- #if qDebug
- static bool gBreakOnThrow;
- static bool gLogExceptions;
- #endif
- };
-
- //------------------------------------------------------------------------------
-
- class SilentException : public Exception
- {
- public:
- SilentException(const SilentException& exc) : Exception(exc) {}
- SilentException(const LocationInCode& where,
- OSStatus status) : Exception(where, status) {}
- virtual ~SilentException();
-
- virtual void Log() const;
- virtual void Report() const;
- virtual bool IsSilent(void) const;
- virtual OSErr GetAEParams(AERecord& evt, bool create) const;
-
- private:
- typedef Exception Inherited;
- };
-
- //------------------------------------------------------------------------------
-
- class StdException : public Exception
- {
- protected:
- AEDesc fErrParams; // AERecord contianing error info to return
-
- public:
- StdException(const StdException& exc);
-
- StdException(
- const LocationInCode& where,
- OSStatus status = eGeneralErr)
- : Exception(where, status)
- {
- fErrParams.descriptorType = typeNull;
- fErrParams.dataHandle = nil;
- }
-
- virtual ~StdException();
-
- virtual void Log() const;
- virtual OSErr GetAEParams(AERecord& evt, bool create) const;
- virtual bool GetErrorMessage(AEDesc& msg) const;
- virtual bool PutErrorString(AERecord& evt) const;
-
- virtual bool CanAddErrorParam(AEKeyword param, bool overwrite);
- virtual bool HasErrorParam(AEKeyword param) const;
- virtual void PutErrorParamPtr(
- AEKeyword param,
- DescType type,
- const void* data,
- Size size,
- bool overwrite = false);
- virtual void PutErrorParamDesc(
- AEKeyword param,
- const AEDesc& desc,
- bool overwrite = false);
-
- inline AEDesc& GetErrorParams() { return fErrParams; }
-
- private:
- typedef Exception Inherited;
- };
-
- //
- // Convenience functions
- //
- // (These want to be inlines, but must be macros to get __FILE__ and __LINE__ to evaluate to
- // useful values in the Throw() macros)
- //
-
- inline OSErr ClipToOSErr(OSStatus val) { return (val <= SHRT_MAX && val >= SHRT_MIN) ? (OSErr) val : eGeneralErr; }
-
- #ifndef __NO_LOCATION_MACROS__
-
- // Macros to get __FILE__ and __LINE__ correct
-
- #define Throw GetLocationInCode().Throw
- #define ThrowTypeError GetLocationInCode().ThrowTypeError
- #define ThrowKeywordError GetLocationInCode().ThrowKeywordError
- #define LogError GetLocationInCode().LogError
- // #define Failure GetLocationInCode().Failure
- #define DelegateAECall GetLocationInCode().DelegateAECall
-
- #define FailOSStatus GetLocationInCode().FailOSStatus
- #define FailOSErr GetLocationInCode().FailOSErr
- #define FailNIL GetLocationInCode().FailNIL
- #define FailNILResource GetLocationInCode().FailNILResource
- #define ExpectedError GetLocationInCode().ExpectedError
-
- #define Failure GetLocationInCode().Failure
-
- #define FailQDErr GetLocationInCode().FailQDErr
- #define FailAEResult GetLocationInCode().FailAEResult
- #define FailAEKeyword GetLocationInCode().FailAEKeyword
- #define FailOptionalAEKeyword GetLocationInCode().FailOptionalAEKeyword
-
- #define LogIfErr GetLocationInCode().LogIfErr
- #define LogIfNotErr GetLocationInCode().LogIfNotErr
-
- #define FailResError() FailOSErr(ResError());
- #define FailMemError() FailOSErr(MemError());
-
- #define UserCanceled GetLocationInCode().UserCanceled
-
- //
- // Requirements must always be met, but Assertions can be removed in non-debug builds
- // as they are statements that are ASSUMED to always be true
- //
- // The log variants can be used where it is not acceptable to throw an error
- //
- // DebugWarn will break into the debugger with an explanation in debug builds
- //
-
- #define Warning GetLocationInCode().Warning
- #define WarnIf GetLocationInCode().WarnIf
-
- #define Assert GetLocationInCode().Assert
- #define AssertPtrValid GetLocationInCode().AssertPtrValid
- #define AssertHandleValid GetLocationInCode().AssertHandleValid
-
- #define Require GetLocationInCode().Require
- #define RequirePtrValid GetLocationInCode().RequirePtrValid
- #define RequireHandleValid GetLocationInCode().RequireHandleValid
-
- #endif __NO_LOCATION_MACROS__
-
- /*
- void ReportException(Exception& exc, short action_code = 0, const unsigned char *message = nil);
- void ReportOSError(OSErr err, short action_code = 0, const unsigned char *message = nil);
- */
-
- /*
- typedef void (*NotifyFailureProc)(OSErr err);
-
- void SetExceptionNotifyProc(NotifyFailureProc notifyProc);
- Boolean ExceptionNotifyProcInstalled(void);
- void NotifyFailure(OSErr err);
- */
-
-
-
- #endif __EXCEPTIONS__
-